<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Title</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<style>
    form{
        padding: 20px;
        height: auto;
        background-color: skyblue;
        width: 400px;
        margin: 50px auto;
    }
</style>
<%--<c:if test="${sessionScope.uname == null}">
    ${pageContext.response.sendRedirect("./login.jsp")}
</c:if>--%>
<body>
  <form action="" method="post">
      <div class="form-group">
          姓名：<input type="text" id="name" name="name" class="form-control" placeholder="姓名">
      </div>
      <div class="form-group">
          性别：
      <select id="sex" class="form-control">
          <option value="0" selected>男</option>
          <option value="1">女</option>
      </select>
      </div>
      <div class="form-group">
          学号：<input type="text" id="study_number" name="study_number" class="form-control" placeholder="学号">
      </div>
      <div class="form-group">
          联系方式：<input type="text" id="phone" name="phone" class="form-control" placeholder="联系方式">
      </div>
      <div class="form-group">
          家庭住址：<input type="text" id="address" name="address" class="form-control" placeholder="家庭住址">
      </div>
      <button type="button" id="updateOrAdd" class="btn btn-primary">提交</button>
      <button type="button" id="back" class="btn btn-info">返回</button>
  </form>
</body>
<script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>
<script>
    var id = window.location.search.substring(4);
    $.ajax({
        url : "http://localhost:8080/stusys/student/toStuInfo?id="+id,
        dataType: "json",
        contentType: "application/json",
        success:function (data) {
                $("#name").val(data.name);
                $("#study_number").val(data.study_number);
                $("#phone").val(data.phone);
                $("#address").val(data.address);
            if(data.sex == 1){
                    $("#sex option").eq(0).prop("selected",false);
                    $("#sex option").eq(1).prop("selected",true)
                }
            $("#back").click(function () {
                location.href="index.jsp";
            })

            $("#updateOrAdd").click(function () {
                var urlPath = "";
                console.log(data.id);
                if(data.id != undefined){
                    urlPath = "http://localhost:8080/stusys/student/updateStudent";
                }else{
                    urlPath = "http://localhost:8080/stusys/student/addStudent"
                }
                if($("#name").val() == ""){
                    alert("姓名不能为空")
                }else if($("#study_number").val() == ""){
                    alert("学号不能为空")
                }else if($("#phone").val() == ""){
                    alert("联系方式不能为空")
                }else if($("#address").val() == ""){
                    alert("地址不能为空")
                }else{
                    $.ajax({
                        url:urlPath,
                        type:"post",
                        data:{
                            id:data.id,
                            name:$("#name").val(),
                            study_number: $("#study_number").val(),
                            phone:$("#phone").val(),
                            address:$("#address").val(),
                            sex:$("#sex").val()
                        },
                        success:function (data) {
                            alert(data);
                            location.href="index.jsp";
                        }
                    })
                }
            })
        }

    })
</script>
</html>